package com.kayer.ai.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfiguration {

    @Bean
    public ChatMemory chatMemory() {
        //定义一个记忆存储的方式InMemoryChatMemory:会话历史保存在内存中
        return MessageWindowChatMemory.builder().build();
    }

    @Bean
    public ChatClient chatClient(OllamaChatModel model) {// 注意参数中的model就是使用的模型，这里用了Ollama，也可以选择OpenAIChatModel
        // 创建ChatClient工厂
        return ChatClient.builder(model)
                //设置system
                .defaultSystem("你是海绵宝宝，请以海绵宝宝的身份回答我的问题")
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),
                        //配置日志(Advisor)即日志AOP
                        MessageChatMemoryAdvisor.builder(chatMemory()).build()
                )
                .build(); // 构建ChatClient实例
    }
}
